package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.DefinedView.MyAlertDialog;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.StringUtil;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.home.activity.sell.PublishedActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * 個人狀態
 * Created by cumt on 2017/6/20.
 */

public class PersonStateActivity extends BaseActivity implements View.OnClickListener {

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonStateActivity.class);

        activity.startActivityForResult(intent,200);

    }

    private Activity activity;
    private ImageView imgBack;
    private TextView txtTitle;

    private TextView btn_change_state_ok,person_state_count;
    private EditText et_state;

    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;


    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_state);
    }

    @Override
    public void initView() {

        activity=PersonStateActivity.this;
        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("个人狀態");

        person_state_count = (TextView)findViewById(R.id.person_state_count);

        et_state= (EditText)findViewById(R.id.et_state);
        btn_change_state_ok= (TextView)findViewById(R.id.btn_change_state_ok);

        PersonalNomalInfo user = LocalInfo.getUserInfo(PersonStateActivity.this);
        params = new HashMap<>();
        params.put("mobile",user.getMobile());

        initRequest();
    }

    @Override
    public void setListener() {

        TextWatcher nwname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                person_state_count.setText(String.format("%s/30",String.valueOf(et_state.length())));

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(et_state.getText().length()>30){

                }
                else{
                    params.put("status",String.valueOf(et_state.getText()));


                }


            }
        };
        et_state.addTextChangedListener(nwname);

    }

    @Override
    public void doEvent() {

        btn_change_state_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                httpModel.startRequest();
            }
        });

    }

    private void initRequest(){
        httpModel = new HttpModel(RequestUrl.baseUrl+RequestUrl.updateUserUrl,params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if(VilidateUtil.isSuccess(resultCode)){

                    Toast.makeText(PersonStateActivity.this, "修改成功",
                            Toast.LENGTH_SHORT).show();
                    final Intent intent=new Intent();
                    intent.putExtra("status", String.valueOf(params.get("status")));
                    PersonStateActivity.this.setResult(RESULT_OK,intent);
                    finish();
                }
                else{
                    Toast.makeText(PersonStateActivity.this, resultMsg+",修改失敗",
                            Toast.LENGTH_SHORT).show();
                }


            }
        },true);


    }
}
